#!/usr/bin/python

import os,sys
import re
import glob

def ProcSourceFile(filename, newfilename):
    fp=open(filename,'r');
    lines=fp.readlines()
    fp.close()
    
    addInfoLino = [];
    for n in range(len(lines)-1):
        # change ID
        if re.match(r' \* @\(#\)\$Id:[^\n]*$\n', lines[n]):
            lines[n] = r' * @(#)$Id$' + '\n'
        # change 2003 to 2008
        if re.match(r' \* \(c\)  PIONEER CORPORATION  2003', lines[n]):
            lines[n] = r' * (c)  PIONEER CORPORATION  2008' + '\n'
            
        ##find class rem
        #if re.match(r'/\*',lines[n]) and re.match(r' \*\s+class\s+',lines[n+1]):
            #m=n
            #while not (re.search(r'\*/',lines[m])):
                #m=m+1
            #addInfoLino.insert(0,(n,m))
            
        #fine class rem by class definition
        if re.match(r'\s*class\s+[^;]*\n', lines[n]):
            if lines[n-1].find(r'*/')>0 and lines[n-1].find(r'/*')<0:
                remEnd = n-1;
                remStart = n-1;
                while lines[remStart].find(r'/*')<0:
                    remStart = remStart -1
                addInfoLino.insert(0, (remStart,remEnd));
            elif lines[n-2].find(r'*/')>0 and lines[n-2].find(r'/*')<0:
                remEnd = n-2;
                remStart = n-2;
                while lines[remStart].find(r'/*')<0:
                    remStart = remStart -1
                addInfoLino.insert(0, (remStart,remEnd));
            elif lines[n-3].find(r'*/')>0 and lines[n-3].find(r'/*')<0:
                remEnd = n-3;
                remStart = n-3;
                while lines[remStart].find(r'/*')<0:
                    remStart = remStart -1
                addInfoLino.insert(0, (remStart,remEnd));
            
        # remove Log
        if re.match(r'/\*', lines[n]) and re.match(r' \*\s+\$Log',lines[n+1]):
            m=n
            while not (re.search(r'\*/', lines[m])):
                lines[m]=''
                m=m+1
            lines[m]=''
            
            
    for pls in addInfoLino:
        n,m = pls
        for ln in range(n,m):
            if re.search('@author', lines[ln]) or re.search('@version', lines[ln]) or re.search('@date', lines[ln]):
                lines[ln]=''
        lines.insert(m, r' * @date $Date::                    $'+'\n')
        lines.insert(m, r' * @version $Revision$'+'\n')
        lines.insert(m, r' * @author $Author$'+'\n')
                       

    if re.search(r'\.h\s*$', filename):    # if this is a .h file, add #endif rem
        Ifndef = [];
        for n in range(len(lines)):
            if lines[n].startswith(r'#if'):
                Ifndef.append(n);
            elif lines[n].startswith(r'#endif'):
                if len(Ifndef)==1 and lines[Ifndef[0]].startswith(r'#ifndef'):
                    ndefVar = lines[Ifndef[0]].split()[-1]
                    if lines[n].find(ndefVar)<0:
                        lines[n] = r'#endif ' + '/* ' + ndefVar + r' */' + '\n'
                Ifndef.pop()
                    
            
        
    # remove empty lines ''
    for n in range(lines.count('')):
        lines.remove('')
        
    fp = open(newfilename, 'w')
    fp.writelines(lines)
    fp.close();
    
def main():
    if len(sys.argv)==1:
        print "usage: CvtSrc.py sourcefile1 sourcefile2 ..."
    else:
        for gfn in sys.argv[1:]:
            for filename in glob.glob(gfn):
                print "processing " + filename
                ProcSourceFile(filename,filename)
        print "Convert finished."
                
        
    
if __name__ == '__main__':
    main() 
    #ProcSourceFile(r'stdafx.h', r'stdafx.h')
    
    
    